#include "user_extern.h"
#include "citylib.h"

static uint8_t subidx = 0;
static int16_t city_min = 1; /* 当前省份下属城市的最小编码 */
static int16_t city_max = 1; /* 当前省份下属城市的最大编码 */

/*********************************************************************
 *  名称： win9_setting_weather_display
 *  功能： setting_weather 显示接口
 *  参数： void
 *  返回值：win_ret 窗口显示状态
 ********************************************************************/
uint8_t win9_setting_weather_display(void)
{
    uint8_t win_ret = WINDOWS_STATUS_DIS_ONLY;

    char tmo_str[10] = {0};
    char pro_str[10] = {0};
    int16_t min, max = 0;
    
    ui.draw_sys_title(const_setting_weather);
    code_to_provin_name(weather.province_code, pro_str);
    oled.show_string(POSITION_CENTER(pro_str, STR_SIZE_16), 16, T(pro_str), STR_SIZE_16, (subidx == 0) ? 0 : 1);

    code_to_city_name(weather.city_code, tmo_str);
    oled.show_string(POSITION_CENTER(tmo_str, STR_SIZE_16), 32, T(tmo_str), STR_SIZE_16, (subidx == 1) ? 0 : 1);

    get_start_and_end_city_code(weather.province_code, &min, &max);
    sprintf(tmo_str, "%d/%d", (weather.city_code - min + 1), max - min + 1);

    oled.show_string(3, 50, T(tmo_str), STR_SIZE_12, 1);
    oled.show_string(POSITION_RIGHT("OK", STR_SIZE_16), 48, T("OK"), STR_SIZE_16, (subidx == 2) ? 0 : 1);

    return win_ret;
}

/*********************************************************************
 *  名称： win9_setting_weather_btn_select
 *  功能： setting_weather 界面 选择按钮触发，选择下一个可选项
 *  参数： void
 *  返回值：0
 ********************************************************************/
uint8_t win9_setting_weather_btn_select(void *arg)
{
    subidx++;
    if (subidx > 2)
    {
        subidx = 0;
    }
    return 0;
}

/*********************************************************************
 *  名称： win9_setting_weather_btn_enter
 *  功能： setting_tone 界面 确定按钮触发, 当前下一个 省份/城市/保存退出
 *  参数： void
 *  返回值：WINDOWS_STATUS_INIT 下一次显示状态
 ********************************************************************/
uint8_t win9_setting_weather_btn_enter(void *arg)
{
    /* 下一个省份 */
    if (subidx == 0)
    {
        weather.province_code++;
        if (weather.province_code > 34)
        {
            weather.province_code = 1;
        }
        get_start_and_end_city_code(weather.province_code, &city_min, &city_max);
        weather.city_code = city_min;
    }
    /* 下一个城市 */
    if (subidx == 1)
    {
        weather.city_code++;
        if (weather.city_code > city_max)
        {
            weather.city_code = city_min;
        }
    }
    /* 保存退出 */
    if (subidx == 2)
    {
        subidx = 0;
        flash_set_value_i8(SYSTEM_VALUE, SYSTEM_WEATHER_PROVINCE, weather.province_code);
        flash_set_value_i16(SYSTEM_VALUE, SYSTEM_WEATHER_CITY, weather.city_code);
        Dsystem.windows_cur_id = WIN_UI_SETTING;
        return WINDOWS_STATUS_INIT;
    }
    return WINDOWS_STATUS_DIS_ONLY;
}

/*********************************************************************
 *  名称： win9_setting_weather_btn_exit
 *  功能： setting_tone 界面 退出按钮触发, 当前上一个 省份/城市
 *  参数： void
 *  返回值：WINDOWS_STATUS_INIT 下一次显示状态
 ********************************************************************/
uint8_t win9_setting_weather_btn_exit(void *arg)
{
    /* 上一个省份 */
    if (subidx == 0)
    {
        weather.province_code--;
        if (weather.province_code < 0)
        {
            weather.province_code = 34;
        }
        get_start_and_end_city_code(weather.province_code, &city_min, &city_max);
        weather.city_code = city_min;
    }
    /* 上一个城市 */
    if (subidx == 1)
    {
        weather.city_code--;
        if (weather.city_code < city_min)
        {
            weather.city_code = city_max;
        }
    }
    return WINDOWS_STATUS_DIS_ONLY;
}
